/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> levelOrderBottom(TreeNode* root) {
        vector<vector<int>> result;
        vector<int> order;
        queue<TreeNode*> q;
        if(root) q.push(root);
        int sum=1;
        while(!q.empty()){
            while(sum--){
                TreeNode* cur=q.front();
                q.pop();
                order.push_back(cur->val);
                if(cur->left) q.push(cur->left);
                if(cur->right) q.push(cur->right);
            }
            result.push_back(order);
            order.clear();
            sum=q.size();
        }
        reverse(result.begin(),result.end());
        return result;
    }
};